package optiondialoges;

import javax.swing.JPanel;
import java.awt.Frame;
import java.awt.BorderLayout;
import javax.swing.JDialog;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import javax.swing.JLabel;
import javax.swing.JTextField;


import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.Serializable;
import javax.swing.ButtonGroup;
import javax.swing.JRadioButton;

import panel.GlyphPanel;

public class LowPassFilterOptionDialog extends JDialog implements
		ActionListener, WindowListener, Serializable {

	private static final long serialVersionUID = 1L;
	private JPanel jContentPane = null;
	private JPanel buttonPanel = null;
	private JPanel optionPanel = null;
	private JButton okButton = null;
	private JButton cancelButton = null;
	private JLabel widthLabel = null;
	private JTextField widthTextField = null;
	private JLabel heightLabel = null;
	private JTextField heightTextField = null;
	private JLabel radiusLabel = null;
	private JTextField radiusTextField = null;
	private GlyphPanel panel;
	private String width;
	private String height;
	private String radius;
	private JRadioButton lowPassRadioButton = null;
	private JRadioButton highpassRadioButton = null;
	private boolean lowPass;
	/**
	 * @param owner
	 */
	public LowPassFilterOptionDialog(Frame owner, GlyphPanel panel) {
		super(owner);
		this.addWindowListener(this);
		this.panel = panel;
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(224, 220);
		this.setContentPane(getJContentPane());
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(new BorderLayout());
			jContentPane.add(getButtonPanel(), BorderLayout.SOUTH);
			jContentPane.add(getOptionPanel(), BorderLayout.CENTER);
		}
		return jContentPane;
	}

	/**
	 * This method initializes buttonPanel
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getButtonPanel() {
		if (buttonPanel == null) {
			GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
			gridBagConstraints1.gridx = 1;
			gridBagConstraints1.insets = new Insets(0, 10, 10, 10);
			gridBagConstraints1.gridy = 0;
			GridBagConstraints gridBagConstraints = new GridBagConstraints();
			gridBagConstraints.insets = new Insets(0, 10, 10, 10);
			buttonPanel = new JPanel();
			buttonPanel.setLayout(new GridBagLayout());
			buttonPanel.add(getOkButton(), gridBagConstraints);
			buttonPanel.add(getCancelButton(), gridBagConstraints1);
		}
		return buttonPanel;
	}

	/**
	 * This method initializes optionPanel
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getOptionPanel() {
		if (optionPanel == null) {
			GridBagConstraints gridBagConstraints8 = new GridBagConstraints();
			gridBagConstraints8.gridx = 1;
			gridBagConstraints8.anchor = GridBagConstraints.WEST;
			gridBagConstraints8.gridy = 4;
			GridBagConstraints gridBagConstraints9 = new GridBagConstraints();
			gridBagConstraints9.gridx = 1;
			gridBagConstraints9.anchor = GridBagConstraints.WEST;
			gridBagConstraints9.gridy = 3;
			GridBagConstraints gridBagConstraints7 = new GridBagConstraints();
			gridBagConstraints7.fill = GridBagConstraints.VERTICAL;
			gridBagConstraints7.gridy = 2;
			gridBagConstraints7.weightx = 1.0;
			gridBagConstraints7.anchor = GridBagConstraints.WEST;
			gridBagConstraints7.gridx = 1;
			GridBagConstraints gridBagConstraints6 = new GridBagConstraints();
			gridBagConstraints6.gridx = 0;
			gridBagConstraints6.insets = new Insets(0, 0, 0, 10);
			gridBagConstraints6.anchor = GridBagConstraints.EAST;
			gridBagConstraints6.gridy = 2;
			radiusLabel = new JLabel();
			radiusLabel.setText("Radius");
			GridBagConstraints gridBagConstraints5 = new GridBagConstraints();
			gridBagConstraints5.fill = GridBagConstraints.VERTICAL;
			gridBagConstraints5.gridy = 1;
			gridBagConstraints5.weightx = 1.0;
			gridBagConstraints5.insets = new Insets(0, 0, 10, 0);
			gridBagConstraints5.anchor = GridBagConstraints.WEST;
			gridBagConstraints5.gridx = 1;
			GridBagConstraints gridBagConstraints4 = new GridBagConstraints();
			gridBagConstraints4.gridx = 0;
			gridBagConstraints4.insets = new Insets(0, 20, 10, 10);
			gridBagConstraints4.anchor = GridBagConstraints.EAST;
			gridBagConstraints4.gridy = 1;
			heightLabel = new JLabel();
			heightLabel.setText("Height");
			GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
			gridBagConstraints3.fill = GridBagConstraints.VERTICAL;
			gridBagConstraints3.gridy = 0;
			gridBagConstraints3.weightx = 1.0;
			gridBagConstraints3.insets = new Insets(0, 0, 10, 0);
			gridBagConstraints3.anchor = GridBagConstraints.WEST;
			gridBagConstraints3.gridx = 1;
			GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
			gridBagConstraints2.gridx = 0;
			gridBagConstraints2.insets = new Insets(0, 20, 10, 10);
			gridBagConstraints2.anchor = GridBagConstraints.EAST;
			gridBagConstraints2.gridy = 0;
			widthLabel = new JLabel();
			widthLabel.setText("Width");
			optionPanel = new JPanel();
			optionPanel.setLayout(new GridBagLayout());
			optionPanel.add(widthLabel, gridBagConstraints2);
			optionPanel.add(getWidthTextField(), gridBagConstraints3);
			optionPanel.add(heightLabel, gridBagConstraints4);
			optionPanel.add(getHeightTextField(), gridBagConstraints5);
			optionPanel.add(radiusLabel, gridBagConstraints6);
			optionPanel.add(getRadiusTextField(), gridBagConstraints7);
			optionPanel.add(getLowPassRadioButton(), gridBagConstraints9);
			optionPanel.add(getHighpassRadioButton(), gridBagConstraints8);
			ButtonGroup group=new ButtonGroup();
			group.add(getHighpassRadioButton());
			group.add(getLowPassRadioButton());
		}
		return optionPanel;
	}

	/**
	 * This method initializes okButton
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getOkButton() {
		if (okButton == null) {
			okButton = new JButton();
			okButton.setText("Ok");
			okButton.addActionListener(this);
		}
		return okButton;
	}

	/**
	 * This method initializes cancelButton
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getCancelButton() {
		if (cancelButton == null) {
			cancelButton = new JButton();
			cancelButton.setText("Cancel");
			cancelButton.addActionListener(this);
		}
		return cancelButton;
	}

	/**
	 * This method initializes widthTextField
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getWidthTextField() {
		if (widthTextField == null) {
			widthTextField = new JTextField();
			widthTextField.setPreferredSize(new Dimension(120, 20));
			widthTextField.setText("256");
		}
		return widthTextField;
	}

	/**
	 * This method initializes heightTextField
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getHeightTextField() {
		if (heightTextField == null) {
			heightTextField = new JTextField();
			heightTextField.setPreferredSize(new Dimension(120, 20));
			heightTextField.setText("256");
		}
		return heightTextField;
	}

	/**
	 * This method initializes radiusTextField
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getRadiusTextField() {
		if (radiusTextField == null) {
			radiusTextField = new JTextField();
			radiusTextField.setPreferredSize(new Dimension(120, 20));
			radiusTextField.setText("10");
		}
		return radiusTextField;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		JButton button = (JButton) e.getSource();
		if (button.getText().contentEquals("Ok")) {
			width = widthTextField.getText();
			height = heightTextField.getText();
			radius = radiusTextField.getText();
			lowPass=lowPassRadioButton.isSelected();
			String command = String
					.format(
							" newImage(\"Filter\", \"8-bit black\", %s, %s, 1);%nradius = %s; makeOval(getWidth/2-radius, getHeight/2-radius, radius*2, radius*2);%nsetForegroundColor(255, 255, 255);%nrun(\"Fill\");%nrun(\"Select None\");%n",
							width, height, radius);
			if (highpassRadioButton.isSelected()) {
				command=String.format("%s%nrun(\"Invert\");%n", command);
			}
			command=String.format("%s%nrun(\"Divide...\", \"value=255\");%n", command);
			panel.setOptionsSet(true);
			panel.setDialog(this);
			panel.setCommand(command);

		}else {
			widthTextField.setText(width);
			heightTextField.setText(height);
			radiusTextField.setText(radius);
			lowPassRadioButton.setSelected(lowPass);
		}
		this.setVisible(false);

	}

	@Override
	public void windowActivated(WindowEvent e) {
		// ignore

	}

	@Override
	public void windowClosed(WindowEvent e) {
		// ignore

	}

	@Override
	public void windowClosing(WindowEvent e) {
		// ignore

	}

	@Override
	public void windowDeactivated(WindowEvent e) {
		widthTextField.setText(width);
		heightTextField.setText(height);
		radiusTextField.setText(radius);
	}

	@Override
	public void windowDeiconified(WindowEvent e) {
		// ignore

	}

	@Override
	public void windowIconified(WindowEvent e) {
		// ignore

	}

	@Override
	public void windowOpened(WindowEvent e) {
		width = widthTextField.getText();
		height = heightTextField.getText();
		radius = radiusTextField.getText();
		lowPass=lowPassRadioButton.isSelected();		
	}

	/**
	 * This method initializes lowPassRadioButton	
	 * 	
	 * @return javax.swing.JRadioButton	
	 */
	private JRadioButton getLowPassRadioButton() {
		if (lowPassRadioButton == null) {
			lowPassRadioButton = new JRadioButton();
			lowPassRadioButton.setSelected(true);
			lowPassRadioButton.setText("Low-pass Filter");
		}
		return lowPassRadioButton;
	}

	/**
	 * This method initializes highpassRadioButton	
	 * 	
	 * @return javax.swing.JRadioButton	
	 */
	private JRadioButton getHighpassRadioButton() {
		if (highpassRadioButton == null) {
			highpassRadioButton = new JRadioButton();
			highpassRadioButton.setText("High-pass Filter");
		}
		return highpassRadioButton;
	}

} // @jve:decl-index=0:visual-constraint="10,10"
